#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define in read()

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 2e5+10;

int nodenum,tot,s[N<<5],ls[N<<5],rs[N<<5],n;
ll ans,ans1,ans2;

void upd(int &x,int pos,int l = 1,int r = n){
	if(!x) x = ++nodenum; s[x]++; if(l == r) return;
	int mid = l + r >> 1; if(pos <= mid) upd(ls[x],pos,l,mid);else upd(rs[x],pos,mid+1,r);
}

void merge(int &x,int y){
	if(!x || !y) return x = x | y,void();
	ans1 += 1ll * s[rs[x]] * s[ls[y]]; ans2 += 1ll * s[ls[x]] * s[rs[y]]; s[x] += s[y];
	merge(ls[x],ls[y]); merge(rs[x],rs[y]);
}

void dfs(int &x){
	int ls,rs,v = in; x = 0;
	if(!v){
		dfs(ls); dfs(rs); ans1 = ans2 = 0; merge(x = ls,rs); ans += min(ans1,ans2);
	}else upd(x,v);
}

int main (){
	n = in; int rt = 0; dfs(rt); printf("%lld\n",ans);
	return 0;
}
